package com.icss.head.servlet;

import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.icss.common.servlet.BaseServlet;
import com.icss.common.util.PageInfoUtil;
import com.icss.common.vo.ApiResponse;
import com.icss.common.vo.ApiResponseEnum;
import com.icss.service.IOrdersDbService;
import com.icss.service.IProductDbService;
import com.icss.service.IUsersDbService;
import com.icss.service.impl.OrdersDbServiceImpl;
import com.icss.service.impl.ProductDbServiceImpl;
import com.icss.service.impl.UsersDbServiceImpl;
import com.icss.vo.MakeupCart;
import com.icss.vo.MakeupOrders;
import com.icss.vo.MakeupOrdersProd;
import com.icss.vo.MakeupUsers;
import com.icss.vo.MakeupUsersEnum;

/**
 * 订单操作
 * 
 * @author 陈岩
 */
@WebServlet(urlPatterns = "/head/OrdersHeadServlet")
public class OrdersHeadServlet extends BaseServlet
{
	private IOrdersDbService ordersDbService;
	private IProductDbService productDbService ; 
	private IUsersDbService usersDbService ; 
	
	@Override
	public void init(ServletConfig config) throws ServletException
	{
		super.init(config);
		this.productDbService = new ProductDbServiceImpl() ;
		UsersDbServiceImpl usersDbServiceImpl = new UsersDbServiceImpl();
		usersDbServiceImpl.setProductDbService(productDbService);
		
		OrdersDbServiceImpl ordersDbServiceImpl = new OrdersDbServiceImpl();
		ordersDbServiceImpl.setUsersDbService(usersDbServiceImpl);
		ordersDbServiceImpl.setProductDbService(productDbService);
		
		this.usersDbService = usersDbServiceImpl;
		this.ordersDbService = ordersDbServiceImpl ; 
	}
	
	@Override
	protected void service(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException
	{
		/* 接收参数 */
		String method = request.getParameter("method");
		if ("ordersList".equalsIgnoreCase(method))
		{
			/* ordersList */
			ordersList(request, response);
		} else if ("ordersInsert".equalsIgnoreCase(method))
		{
			/* ordersInsert */
			ordersInsert(request, response);
		}else if ("ordersInsertSubmit".equalsIgnoreCase(method))
		{
			/* ordersInsertSubmit */
			ordersInsertSubmit(request, response);
		} else if ("ordersUpdate".equalsIgnoreCase(method))
		{
			/* ordersUpdate  */
			ordersUpdate(request, response);
		}else if ("ordersProductUpdate".equalsIgnoreCase(method))
		{
			/* ordersProductUpdate */
			ordersProductUpdate(request, response);
		}else if ("ordersProductList".equalsIgnoreCase(method))
		{
			/* ordersProductList */
			ordersProductList(request, response);
		} else if ("ordersProductUpdate".equalsIgnoreCase(method))
		{
			/* ordersProductUpdate */
			ordersProductUpdate(request, response);
		}
	}
	
	/**
	 * 订单列表
	 * @param request
	 * @param response
	 * @throws ServletException
	 * @throws IOException
	 */
	public void ordersList(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
	{
		MakeupUsers users = (MakeupUsers) this.findObjfromSession(request, "users");
		/* 分页信息 */
		PageInfoUtil pageInfoUtil = this.proccedPageInfo(request);
		/* 搜索条件,生成公共的搜索条件 */
		Map<String, Object> condMap = this.proccedSearch(request);
		condMap.put("usersId", users.getId());
		condMap.put("extend", "true");
		/* 查询数据库
		 * 所有的数据以JSON的形式返回
		 *  */
		ApiResponse<MakeupOrders> apiResponse = this.ordersDbService.findCondListOrdersService(pageInfoUtil, condMap);
		/* 将结果存储到Request中 */
		request.setAttribute("response", apiResponse.toJSON());
		
		/* 页面跳转 */
		String path =  "/head/orders/ordersList" ; 
		path = this.prefix + path + this.subfix; 
		request.getRequestDispatcher(path).forward(request, response);
	}

	/**
	 * 打开下单页面
	 * @param request
	 * @param response
	 */
	public void ordersInsert(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
	{
		/* 准备路径 */
		MakeupUsers users = (MakeupUsers) this.findObjfromSession(request, "users");
		/*========查询购物车=========*/
		/* 分页信息 */
		/* 搜索条件,生成公共的搜索条件 */
		Map<String, Object> condMap = this.proccedSearch(request);
		/* 查询我的,查询启用的购物车 */
		condMap.put("usersId", users.getId());
		condMap.put("extend", "true");
		/* 查询数据库
		 * 所有的数据以JSON的形式返回
		 *  */
		ApiResponse<MakeupCart> apiResponse = this.usersDbService.findCondListCartService(null, condMap);
		/* 将结果存储到Request中 */
		request.setAttribute("response", apiResponse.toJSON());
		
		/* 页面跳转 */
		String path =  "/head/orders/ordersInsert" ; 
		path = this.prefix + path + this.subfix; 
		request.getRequestDispatcher(path).forward(request, response);
	}
	
	/**
	 * 下单提交
	 * @param request
	 * @param response
	 * @throws ServletException
	 * @throws IOException
	 */
	public void ordersInsertSubmit(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
	{
		String path = "/head/OrdersHeadServlet?method=ordersList";
		MakeupUsers users = (MakeupUsers) this.findObjfromSession(request, "users");
		/*========查询购物车=========*/
		/* 分页信息 */
		/* 搜索条件,生成公共的搜索条件 */
		Map<String, Object> condMap = this.proccedSearch(request);
		/* 查询我的,查询启用的购物车 */
		condMap.put("usersId", users.getId());
		condMap.put("status", MakeupUsersEnum.STATUS_ENABLE.getStatus());
		condMap.put("extend", "true");
		/* 查询数据库
		 * 所有的数据以JSON的形式返回
		 *  */
		ApiResponse<MakeupCart> apiResponse = this.usersDbService.findCondListCartService(null, condMap);
		/* 将结果存储到Request中 */
		request.setAttribute("response", apiResponse.toJSON());
		
		/* 接收必要信息 */
		String name = request.getParameter("name");
		String phone = request.getParameter("phone");
		String address = request.getParameter("address");
		String note = request.getParameter("note");
		
		/* 创建对象 */
		MakeupOrders orders = new MakeupOrders();
		orders.setUsersId(users.getId());
		orders.setName(name);
		orders.setPhone(phone);
		orders.setAddr(address);
		orders.setNote(note);
		
		/* 设置购物车 */
		List<MakeupCart> cartList = apiResponse.getDataListJava();
		orders.setCartList(cartList);
		
		ApiResponse<Object> apiResponseDb = this.ordersDbService.saveOneOrdersService(orders);
		if(apiResponseDb.getCode() == ApiResponseEnum.STATUS_SUCCESS.getStatus())
		{
			response.sendRedirect(request.getContextPath() + path);
			return ; 
		}
		this.ordersInsert(request, response);
	}

	/**
	 * 订单更新
	 * @param request
	 * @param response
	 */
	public void ordersUpdate(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
	{
		String path = "/head/orders/ordersInfo";
		/* 准备路径 */
		Map<String, Object> condMap = new HashMap<String, Object>();
		
		/* 按照id查询 */
		String id = request.getParameter("id");
		condMap.put("id", id);
		condMap.put("extend", "true");
		ApiResponse<MakeupOrders> apiResponse = this.ordersDbService.findOneOrdersService(condMap);
		request.setAttribute("response", apiResponse.toJSON());
		
		/* 同时查询出订单商品信息 */
		MakeupOrders orders = apiResponse.getDataOneJava();
		condMap.clear();
		condMap.put("ordersId", orders.getId());
		condMap.put("extend", "true");
		ApiResponse<MakeupOrdersProd> ordersProductApiResponse = this.ordersDbService.findCondListOrdersProductService(null, condMap);
		request.setAttribute("ordersProductResponse", ordersProductApiResponse.toJSON());
		
		/* 根据不同的判断处理不同的类型 */
		String operType = request.getParameter("operType");
		if("update".equalsIgnoreCase(operType))
		{
			path = "/head/orders/ordersUpdate" ; 
		}
		
		path = this.prefix + path + this.subfix; 
		request.getRequestDispatcher(path).forward(request, response);
	}

	/**
	 * 订单商品列表
	 * @param request
	 * @param response
	 * @throws ServletException
	 * @throws IOException
	 */
	public void ordersProductList(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
	{
		String path = "/head/orders/ordersProductList";
		/* 分页信息 */
		PageInfoUtil pageInfoUtil = this.proccedPageInfo(request);
		/* 搜索条件,生成公共的搜索条件 */
		Map<String, Object> condMap = this.proccedSearch(request);
		String ordersId = request.getParameter("ordersId");
		if(ordersId == null)
		{
			ordersId = "" ; 
		}
		request.setAttribute("ordersId", ordersId);
		condMap.put("ordersId", ordersId);
		
		/* 扩展信息 */
		condMap.put("extend", "true");
		/* 查询数据库
		 * 所有的数据以JSON的形式返回
		 *  */
		ApiResponse<MakeupOrdersProd> apiResponse = this.ordersDbService.findCondListOrdersProductService(pageInfoUtil, condMap);
		/* 将结果存储到Request中 */
		request.setAttribute("response", apiResponse.toJSON());
		
		/* 页面跳转 */
		path = this.prefix + path + this.subfix; 
		request.getRequestDispatcher(path).forward(request, response);
	}

	/**
	 * 订单商品更新
	 * @param request
	 * @param response
	 */
	public void ordersProductUpdate(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
	{
		/* 准备路径 */
		Map<String, Object> condMap = new HashMap<String, Object>();
		
		/* 按照id查询 */
		String id = request.getParameter("id");
		condMap.put("id", id);
		condMap.put("extend", "true");
		ApiResponse<MakeupOrdersProd> apiResponse = this.ordersDbService.findOneOrdersProductService(condMap);
		request.setAttribute("response", apiResponse.toJSON());
		
		/* 根据不同的判断处理不同的类型 */
		String operType = request.getParameter("operType");
		/* 页面跳转 */
		String path = "/head/orders/ordersProductInfo";
		path = this.prefix + path + this.subfix; 
		request.getRequestDispatcher(path).forward(request, response);
	}
}
